<?php
namespace  Home\Model;
use    Think\Model;
class CartModel extends Model{
    public function moveDataToDb()
	{
		$id = session('member_id');
		//var_dump($id);die();
		if($id)
		{
			// 从COOKIE中取出商品
			$cart = isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) : array();
			//var_dump($cart);die();
			// 循环每件商品插入到数据库中
			foreach ($cart as $k => $v)
			{
				
				$this->addToCart($k, $v); // 把商品插入到数据库中
			}
			// 清空COOKIE
			setcookie('cart', '', time()-1, '/', C('CART_COOKIE_DOMAIN'));
		}
	}
	//清空购物车
	public function clear(){
		$id = session('member_id');
		$this->where(array(
			'member_id'=>array('eq',$id),
		))->delete();
	}
	public function addToCart($goodsId,$goodsNumber){
		$goodsId = (int)$goodsId;
		$goodsNumber = (int)$goodsNumber;
		//var_dump($goodsId);var_dump($goodsNumber);die();
		if($goodsId < 0 || $goodsNumber<0 ){
			$this->error='商品的数量不能小于0 fuck';
			return false;
		}
		//判断会员有没有登录  如果有登录 判断购物车中有没有商品,如何没有登录 则保存在cookie中
		$id = session('member_id');
		//var_dump($id);die();
		if($id){
			//判断这个人购物车中有没有商品
			$has = $this->where(array(
				'member_id'=>array('eq',$id),
				'goods_id'=>array('eq',$goodsId),
			))->find();
			//var_dump($has);die();
			//如何有商品的话  在这次的基础上 在加上原来的数量 setInc
			if($has){
				$this->where(array(
					'member_id'=>array('eq',$id),
					'goods_id'=>array('eq',$goodsId),
				))->setInc('goodsNumber',$goodsNumber);
			}else{
				$this->add(array(
					'member_id'=>$id,
					'goods_id'=>$goodsId,
					'goods_number'=>$goodsNumber,
				));
			}	
		}else{
			 //如果是未登录的情况下 这保存在cookie中
		    //取出这个数组
			$cart = isset($_COOKIE['cart'])? unserialize($_COOKIE['cart']) : array();
			$key = $goodsId;
			if(isset($cart[$key])){
				$cart[$key] += $goodsNumber;
			}else{
				$cart[$key] = $goodsNumber;
			}
			//再把这个一维数组保持在COOKIE中
			    //天数
				$day = C('CART_COOKIE_EXPIRE'); 
				$expire = time()+$day * 24 *3600;
				//设置
				setcookie('cart',serialize($cart),$day,'/',C(CART_COOKIE_DOMAIN));
		}
		return true;
	}
	
	//获得当前购物车中的商品
	public function cartList(){
		$id = session('member_id');
		//echo $id;die();
		if($id){
			//var_dump($cartData);die();
			$cart = $this->where(array(
				'member_id'=>array('eq',$id),
			))->select();
			//echo '<pre>';
			//var_dump($cart);die();
		}else{
			$_cart = isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) : array();
			//var_dump($_cart);
			// 把这个一维转成二维，和上面的结构一样
			$cart = array();
			foreach ($_cart as $k => $v)
			{
			
				$cart[] = array(
						'goods_id' => $k,
					
						'goods_number' => $v,
				);
			}
		}
		/**********************商品的详细信息**********************/
		 $goodsModel = D('Admin/Goods');
		 
	     foreach ($cart as $k=>$v){
	     	$goodsData = $goodsModel->field('id,goods_name,sm_logo,shop_price')->find($v['goods_id']);
	     	$cart[$k]['goods_name'] = $goodsData['goods_name'];
	     	$cart[$k]['sm_logo'] = $goodsData['sm_logo'];
	     	$cart[$k]['price'] = $goodsData['shop_price'];
	     	//var_dump($goodsData);die();
	     }
	    // echo '<pre>';
	    // var_dump($cart);die();
	     return $cart;
		
	}
	//修改购物车中商品 的数量
	public function editCartNumber($goodsId,$goodsNumber){
		$id = session('member_id');
		if($id){
			if($goodsNumber){
				$this->where(array(
					'goods_id'=>$goodsId,
					'member_id'=>$id,
				))->setField('goods_number', $goodsNumber);
			}
		}else{
			//放到CookIE中
			$cart = isset($_COOKIE['cart'])? unserialize($_COOKIE['cart']) : array();
			$key = $goodsId; 
			$cart[$key] = $goodsNumber;
			$day = C('CART_COOKIE_EXPIRE');
			$expire = time()+ $day*24*3600;
			setcookie('cart', serialize($cart), $expire, '/', C('CART_COOKIE_DOMAIN'));
		}
		return TRUE;
	}
}